﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameFramework.ModelManager.SkinnedModelPipeline
{
    public class SkinnedCharacterAttacher
    {
        public SkinnedCharacterAttacher(SkinnedGameCharacter skinnedModel, Model attachedModel, string where)
        {
            this.skinnedModel = skinnedModel;
            this.attachedModel = attachedModel;
            this.where = where;
            this.index = skinnedModel.Model.Bones[where].Index;
        }

        public void Draw(Matrix view, Matrix projection, Vector3 cameraPosition)
        {
            foreach (ModelMesh mesh in attachedModel.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    Matrix gunTransform = Matrix.CreateScale(0.65f) *
                        Matrix.CreateFromYawPitchRoll(-MathHelper.PiOver2, 0, MathHelper.Pi);

                    SkinnedAnimationPlayer skinnnedAnimationPlayer = 
                        (SkinnedAnimationPlayer)skinnedModel.AnimationPlayer;

                    effect.World = gunTransform *
                        skinnnedAnimationPlayer.WorldTransforms[index];

                    effect.View = view;
                    effect.Projection = projection;
                }

                mesh.Draw();
            }
        }

        SkinnedGameCharacter skinnedModel;
        Model attachedModel;
        string where;
        int index;
    }
}
